package leetcode_day._2022._202210._0110;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 856. 括号的分数
 * 算法：栈 + 模拟
 *
 * @author yzh
 * @version 1.0
 * @date 2022/10/9 18:48
 */
public class _09_856 {

    public int scoreOfParentheses(String s) {
        Deque<Integer> stack = new ArrayDeque<>();
        stack.addLast(0);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') stack.addLast(0);
            else if (s.charAt(i) == ')') {
                int cur = stack.pollLast();
                stack.addLast(stack.pollLast() + Math.max(cur * 2, 1));
            }
        }
        return stack.peekLast();
    }

}
